package com.example.Utility;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件网络传输下载
 */

@Slf4j
@RestController
@RequestMapping("/file")
public class FileTransfer {

    @Value("${Image_path.path}")
    private String this_path;

    @Value("${Image_path.comment_image}")
    private String comment_image;


    /**
     * @param file
     * @param fileName
     * @return Result
     */
    @PostMapping("/update")
    public Result updateFile(MultipartFile file, String fileName) {
        File storage = new File(this_path);
        try {
            //判断存储文件位置是否为空 null 及创建
            if (!storage.exists()) {
                storage.mkdirs();
            }
            file.transferTo(new File(this_path + fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.succeed(fileName);
    }

    /**
     * 上传照片到七牛云
     *
     * @param MultipartFile file, String commentId
     * @throws Exception
     */
    @PostMapping("/updateCommentImage")
    public Result<String> commentImage(MultipartFile file, String commentId) {
        //原始文件名
        String Original = file.getOriginalFilename();
        //         存入文件名
        String substring = UUID.randomUUID() + "/" + commentId + Original.substring(Original.lastIndexOf("."));
        //创建本都文件对象
        File storage = new File(comment_image);
        try {
            //存入指定文件
            //如果指定文件不存在那就创建
            if (!storage.exists()) {
                storage.mkdirs();
            }
            file.transferTo(new File(comment_image + substring));
            return Result.succeed("文件获取成功！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //文件下载
    @GetMapping("down")
    public void DownFile(String fileName, HttpServletResponse response) throws Exception {
        //设置响应类型
        response.setContentType("image/jpeg");
        File file = new File(this_path + fileName);
        //文件 --》  网页    输入流 本地
        FileInputStream fileInputStream = new FileInputStream(file);
        //        网页输出流
        ServletOutputStream servletOutputStream = response.getOutputStream();
        int let = 0;
        //        1mb的byte
        byte[] bytes = new byte[1024];
        //        把每一个二进制数据传输到数组中
        while ((let = fileInputStream.read(bytes)) != -1) {
            servletOutputStream.write(bytes, 0, let);
            servletOutputStream.flush();
        }
        servletOutputStream.close();
        fileInputStream.close();
    }


//    @GetMapping("FileToDesktop")
//    public void FileToDesktop() {
//        FtpUtil fus = new FtpUtil();
//        fus.downloadFtpFile("121.4.84.27", "imageFTP", "123456", 21, "D:\\卓面\\caige\\");
//    }

}

